package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.entity.MmallCategory;
import com.mall.entity.MmallProduct;
import com.mall.entity.vo.CategoryVo;
import com.mall.mapper.MmallCategoryMapper;
import com.mall.service.MmallCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.service.MmallProductService;
import com.mall.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ppmall
 * @since 2021-09-19
 */
@Service
public class MmallCategoryServiceImpl extends ServiceImpl<MmallCategoryMapper, MmallCategory> implements MmallCategoryService {

    @Autowired
    MmallProductService productService;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<CategoryVo> getCategoryNestedList(Boolean withProduct) {
        QueryWrapper<MmallCategory> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", 0);
        List<MmallCategory> categoryOneList = baseMapper.selectList(wrapperOne);
        List<CategoryVo> categoryVoOneList = new ArrayList<>();

        for(MmallCategory categoryOne: categoryOneList) {
            CategoryVo categoryVoOne = new CategoryVo();
            categoryVoOne.setId(categoryOne.getId());
            categoryVoOne.setName(categoryOne.getName());

            QueryWrapper<MmallCategory> wrapperTwo = new QueryWrapper<>();
            wrapperTwo.eq("parent_id", categoryOne.getId());
            List<MmallCategory> categoryTwoList = baseMapper.selectList(wrapperTwo);
            List<CategoryVo> categoryVoTwoList = new ArrayList<>();

            for(MmallCategory categoryTwo: categoryTwoList) {
                CategoryVo categoryVoTwo = new CategoryVo();
                categoryVoTwo.setId(categoryTwo.getId());
                categoryVoTwo.setName(categoryTwo.getName());
                if(withProduct) {
                    QueryWrapper<MmallProduct> wrapperThree = new QueryWrapper<>();
                    wrapperThree.eq("category_id", categoryTwo.getId());
                    List<MmallProduct> categoryThreeList = productService.list(wrapperThree);
                    List<CategoryVo> categoryVoThreeList = new ArrayList<>();

                    for(MmallProduct categoryThree: categoryThreeList) {
                        CategoryVo categoryVoThree = new CategoryVo();
                        categoryVoThree.setId(categoryThree.getId());
                        categoryVoThree.setName(categoryThree.getName());
                        categoryVoThree.setChildren(null);
                        categoryVoThreeList.add(categoryVoThree);
                    }

                    categoryVoTwo.setChildren(categoryVoThreeList);
                } else {
                    categoryVoTwo.setChildren(null);
                }

                categoryVoTwoList.add(categoryVoTwo);
            }

            categoryVoOne.setChildren(categoryVoTwoList);
            categoryVoOneList.add(categoryVoOne);

        }
        System.out.println(categoryVoOneList);
        return categoryVoOneList;
    }

    @Override
    public boolean saveCategory(MmallCategory category) {
        int insert = baseMapper.insert(category);
        if(insert > 0) {
            String redis_cache_name = "allCategory";
            redisUtil.del(redis_cache_name);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean updateCategory(MmallCategory category) {
        int update = baseMapper.updateById(category);
        if(update > 0) {
            String redis_cache_name = "allCategory";
            redisUtil.del(redis_cache_name);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean deleteCategory(int id) {
        QueryWrapper<MmallCategory> categoryWrapper = new QueryWrapper<>();
        categoryWrapper.eq("parent_id", id);
        if(baseMapper.selectCount(categoryWrapper) > 0) {
            return false;
        }

        QueryWrapper<MmallProduct> productWrapper = new QueryWrapper<>();
        productWrapper.eq("category_id", id);
        if(productService.count(productWrapper) > 0) {
            return false;
        }

        int delete = baseMapper.deleteById(id);
        if(delete > 0) {
            String redis_cache_name = "allCategory";
            redisUtil.del(redis_cache_name);
            return true;
        } else {
            return false;
        }
    }
}
